package route

import (
	"gitee.com/DonHz/cloud-file-storage/handler"
	"gitee.com/DonHz/cloud-file-storage/service/upload/api"
	"github.com/gin-gonic/gin"
	"net/http"
)

func Router() *gin.Engine {
	r := gin.Default()
	r.LoadHTMLFiles("./static/view/index.html",
		"./static/view/signup.html",
		"./static/view/signin.html",
		"./static/view/home.html",
		"./static/view/download.html",
		"./static/view/upload.html",
	)
	r.Static("/static", "./static")

	r.GET("/file/upload", func(ctx *gin.Context) {
		ctx.HTML(http.StatusOK, "index.html", gin.H{
			"msg": "ok",
		})
	})
	//r.Use(handler.HTTPInterceptor())
	r.POST("/file/upload", api.UploadHandler)
	r.OPTIONS("/file/upload", func(c *gin.Context) {
		c.Header("Access-Control-Allow-Origin", "*")
		c.Header("Access-Control-Allow-Methods", "POST, OPTIONS")
		c.Status(http.StatusNoContent)
	})

	r.POST("/file/query", handler.GetFileMetaHandler)
	r.GET("/file/download", handler.DownloadHandler)
	r.POST("/file/update", handler.FileMetaUpdateHandler)
	r.POST("/file/delete", handler.FileDeleteHandler)
	r.POST("/file/fastupload", handler.TryFastUploadHandler)
	r.POST("/file/downloadurl", handler.DownloadURLHandler)

	// 分块上传接口
	r.POST("/file/mpupload/init", handler.InitialMultipartUploadHandler)
	r.POST("/file/mpupload/uppart", handler.UploadPartHandler)
	r.POST("/file/mpupload/complete", handler.CompleteUploadHandler)

	return r
}
